package cn.lf.android.lawfirm.home.view;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.ChangeApprovalListActivity;
import cn.lf.android.lawfirm.distribution.DistributionActivity;
import cn.lf.android.lawfirm.distribution.DistributionListActivity;
import cn.lf.android.lawfirm.home.HomeActivity;
import cn.lf.android.lawfirm.home.modle.HomeGroups;
import cn.lf.android.lawfirm.outgoingregistration.OutGoingRegistrationActivity;
import cn.lf.android.lawfirm.undertake.apply.ApplyUndertakeListActivity;
import cn.lf.android.lawfirm.undertake.approval.UndertakeListActivity;
import cn.lf.android.lawfirm.upgrade.approval.UpgradeListActivity;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.worklog.WorkLogListActivity;


public class HomeAdapter extends BaseAdapter {

    private Context mContext;
    private int Tag = 0;

    private List<HomeGroups> homeGroups;
    private int[] mIcons = new int[]{R.mipmap.ayfp,
            R.mipmap.sqlx, R.mipmap.cbsp,
            R.mipmap.bgsp, R.mipmap.zjsq,
            R.mipmap.zjsp, R.mipmap.rzcx1, R.mipmap.wccx1, R.mipmap.quit2};
    private String[] mGroups = new String[]{"案源分派", "承办申请", "承办审批", "变更审批",
            "增级申请", "增级审批", "日志查阅", "外出查询"};

    public HomeAdapter(Context context, List<HomeGroups> homeGroups) {
        this.mContext = context;
        this.homeGroups = homeGroups;
        DebugLog.e("homeGroups==" + homeGroups);
    }

    public void setTag() {
        Tag = 0;
    }

    @Override
    public int getCount() {
        return homeGroups.size();
    }

    @Override
    public Object getItem(int position) {
        return homeGroups.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_home,
                null);
        ImageView imageView = (ImageView) convertView
                .findViewById(R.id.home_grid_item_image);
        TextView txetTextView = (TextView) convertView
                .findViewById(R.id.home_grid_item_text);
        LinearLayout linearLayout = (LinearLayout) convertView
                .findViewById(R.id.linearLayout);
        if (null != homeGroups.get(position)) {
            if ("分派".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[0]);
            } else if ("申请承办".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[1]);
            } else if ("承办审批".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[2]);
            } else if ("变更审批".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[3]);
            } else if ("增级申请".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[4]);
            } else if ("增级审批".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[5]);
            } else if ("日志查阅".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[6]);
            } else if ("外出查询".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[7]);
            } else if ("退出".equals(homeGroups.get(position).getMenumName())) {
                imageView.setImageResource(mIcons[8]);
            }

            if ("分派".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("案源分派");
            } else if ("申请承办".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("承办申请");
            } else if ("承办审批".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("承办审批");
            } else if ("变更审批".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("变更审批");
            } else if ("增级申请".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("增级申请");
            } else if ("增级审批".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("增级审批");
            } else if ("日志查阅".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("日志查阅");
            } else if ("外出查询".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("外出查询");
            } else if ("退出".equals(homeGroups.get(position).getMenumName())) {
                txetTextView.setText("退出");
            }
            linearLayout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    DebugLog.e("homeGroups===" + homeGroups.get(position).getMenuID());
                    startIntent(homeGroups.get(position).getMenuID());
                }
            });
        }

        return convertView;
    }

    private void startIntent(int id) {
        switch (id) {
            case 1://录入案源
//                if (Tag == 0) {
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, DistributionActivity.class), 101);
//                }
//                Tag = 1;
                break;
            case 2://分派
//                if (Tag == 0) {
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, DistributionListActivity.class), 101);
//                }
                break;
            case 3://承办申请
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, ApplyUndertakeListActivity.class), 103);
                break;
            case 4://承办审批
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, UndertakeListActivity.class), 104);
                break;
            case 5://变更审批
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, ChangeApprovalListActivity.class), 105);
                break;
            case 6://

                break;
            case 7://增级审批
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, UpgradeListActivity.class), 107);
                break;
            case 8://日志查询
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, WorkLogListActivity.class), 108);
                break;
            case 9://日志查阅
                Intent intent = new Intent(mContext, WorkLogListActivity.class);
                intent.putExtra("title", "日志查阅");
                intent.putExtra("flag", "1");//日志查阅 传1  工作日志传0
                ((HomeActivity) mContext).startActivityForResult(intent, 109);
                break;
            case 10://外出查询
                ((HomeActivity) mContext).startActivityForResult(new Intent(
                        mContext, OutGoingRegistrationActivity.class), 110);
                break;
            case 11://结案审批
                break;
            case 12://工作流
                break;
            case 13://退出
                ((HomeActivity) mContext).onQuit();
                break;
            default:
                break;
        }
    }

}
